<?php
/*
 * @Author: 偻儸小卒[EdisonLiu_]
 * @Date: 2021-06-23 15:05:51
 * @LastEditors: 偻儸小卒[EdisonLiu_]
 * @LastEditTime: 2021-06-25 11:43:49
 * @Description: 
 */

namespace App\Http\Controllers\AdminApi\Street;

use App\Http\Controllers\Controller;
use App\Http\Models\Admin\ClassifyPermissions;

use App\Http\Models\Admin\Member\Inspection;
// use App\Http\Models\Admin\Street\Personnel;
use App\Http\Models\Admin\Member\Street as Personnel;
use App\Http\Models\Admin\Member\Department as Department;


use App\Http\Models\Admin\Street\Street;
use App\Http\Models\Admin\Street\StreetPersonnel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RoadController extends Controller
{
    // 查列表
    public function list(Request $request)
    {

        
        $limit = $request->get('limit');
        $title = $request->get('title');

        $role = new Street();


        $Street= new Street;
        if(!empty($title)){
            $Street= $Street-> where('name','like','%'.$title.'%');
        }

        $Street= $Street-> with(['hasManyStreetPersonnel','hasManyDepartment'])->paginate($limit);


        return response()->json(['code' => 20000, 'message' => '街道查询成功', 'data' => $Street]);
    }
    // 获取街道人员
    public function personnelList()
    {
        $items = Personnel::get();
        return response()->json(['code' => 20000, 'message' => '角色查询成功', 'data' => $items]);
    }

    // 获取部门人员
    public function departmentList()
    {
        $items = Department::get();
        return response()->json(['code' => 20000, 'message' => '角色查询成功', 'data' => $items]);
    }
    // 查一条
    public function getinfo(Request $request)
    {
        $id = $request->get('id');
        // redaySql(new DB);
        $Street = Street::with(['hasManyStreetPersonnel','hasManyDepartment'])->find($id);
        $Street['personnelid']  = $Street->hasManyStreetPersonnel->pluck('id');
        $Street['departmentid']  = $Street->hasManyDepartment->pluck('id');

        // $personnelid = array();
        // foreach($Street['hasManyStreetPersonnel'] as $k => $v){
        //     array_push($personnelid,$v['id']);
        // }
        // $Street['departmentid'] = $personnelid;
        
        // $departmentid = array();
        // foreach($Street['hasManyDepartment'] as $dk => $dv){
        //     array_push($departmentid,$dv['id']);
        // }
        // $Street['departmentid'] = $departmentid;

        
        return response()->json(['code' => 20000, 'message' => '街道查询成功', 'data' => compact('Street')]);
    }
    // 创建
    public function create(Request $request)
    {
        $Street = new Street;
        $street_name = $request->get('name');
        $personnelid = $request->get('personnelid');

        // 判断街道是否存在
        $role = Street::where(array('name' => $street_name))->get();
        if (!empty(count($role))) {
            return response()->json(['code' => 20001, 'message' => '此街道已存在', 'data' => $role]);
        }
        $array = [
            'name' => $street_name
        ];
        $inser_per = Street::create($array);
        $ress = $inser_per->hasManyStreetPersonnel()->attach($personnelid);

        return response()->json(['code' => 20000, 'message' => '创建街道成功', 'data' => compact('Street')]);
    }

    //修改
    public function update(Request $request)
    {
        $id = $request->get('id');
        if (empty($id)) {
            return response()->json(['code' => 10000, 'message' => '查无此人']);
        }
        $Street = Street::findOrFail($id);
        if (empty($Street)) {
            return response()->json(['code' => 10000, 'message' => '查无此人']);
        }

        $street_name = $request->get('name');
        $personnelid = $request->get('personnelid');
        $departmentid = $request->get('departmentid');
        
        $Street->name=$street_name;
        $Street->update();


        $ress = $Street->hasManyStreetPersonnel()->sync($personnelid);
        $ress = $Street->hasManyDepartment()->sync($departmentid);
        // dd($ress);die;
        return response()->json(['code' => 20000, 'message' => '修改督查员成功', 'data' => compact('Street')]);
    }
    // 删除
    public function delete(Request $request)
    {
        $id = $request->get('id');
        $Street = Street::findOrFail($id);
        $Street->delete();
        return response()->json(['code' => 20000, 'message' => '删除成功', 'data' => $id]);
    }
}
